package com.baseframe.score.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baseframe.mybatis.utils.PageInfo;
import com.baseframe.mybatis.utils.PageUtil;
import com.baseframe.score.entity.SnsScoreRules;
import com.baseframe.score.entity.request.SnsScoreRuleRequest;
import com.baseframe.score.entity.request.SnsScoreUserRequest;
import com.baseframe.score.entity.vo.SnsScoreRulesVO;
import com.baseframe.score.service.SnsScoreRulesService;
import com.baseframe.score.mapper.SnsScoreRulesMapper;
import com.baseframe.tool.result.R;
import com.baseframe.tool.utils.data.FunUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**

* @description 针对表【sns_score_rules(积分规则表)】的数据库操作Service实现
* @createDate 2023-06-26 00:45:34
*/
@Service
public class SnsScoreRulesServiceImpl extends ServiceImpl<SnsScoreRulesMapper, SnsScoreRules>
    implements SnsScoreRulesService{

    @Override
    public Page<SnsScoreRulesVO> snsScorePageList(PageInfo pageInfo, SnsScoreRuleRequest request) {
        Page<SnsScoreRules> snsScoreRulesPage = PageUtil.buildPage(pageInfo, SnsScoreRules.class);
        LambdaQueryWrapper<SnsScoreRules> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FunUtil.isNotEmpty(request.getRewardItem()),SnsScoreRules::getRewardItem,request.getRewardItem());
        queryWrapper.like(FunUtil.isNotEmpty(request.getRewardDesc()),SnsScoreRules::getRewardDesc,request.getRewardDesc());
        queryWrapper.orderByDesc(SnsScoreRules::getCreateTime);

        Page<SnsScoreRules> page = this.page(snsScoreRulesPage, queryWrapper);
        Page<SnsScoreRulesVO> resultPage = new Page<>();
        BeanUtils.copyProperties(page,resultPage,"records");
        List<SnsScoreRulesVO> records = new ArrayList<>();
        for (SnsScoreRules record : page.getRecords()) {
            SnsScoreRulesVO snsScoreRulesVO = new SnsScoreRulesVO();
            BeanUtils.copyProperties(record,snsScoreRulesVO);
            records.add(snsScoreRulesVO);
        }
        resultPage.setRecords(records);
        return resultPage;
    }


}




